myApp.controller('ProjectEditController', function ($scope, $stateParams, $http,$uibModal,ngDialog) {
    $scope.projectName = $stateParams.projectName;
    //0 主干工程  1 分支工程
    $scope.info = {};
    $scope.info.projectType = $stateParams.isTrunk == "" ? "1" : $stateParams.isTrunk;
    //保存工程信息
    $scope.projectInfo = {};
    $scope.projectInfo.packageType = "maven_war";
    //分支名
    $scope.projectInfo.branchName = "";
    $scope.projectInfo.useSonar = false;
    //主干工程列表
    $scope.trunkProjectList = [];
    //已关联的jar工程
    $scope.relateJarList = [];
    //jar工程列表
    $scope.jarProjectList = [];
    $scope.runIng = false;

    $scope.canAddTrunk = function () {
        return arrayContains(getUserRole(), 'admin');
    };

    if ($scope.projectName != '') {
        $http.post(java_url + '/beetle/project/getProject', {
            projectName: $scope.projectName,
            tokenId: getTokenId()
        }).success(function (data) {
            if (data.code != '0') {
                alert("获取工程信息失败!desc:" + data.desc);
                return;
            }
            ;
            $scope.projectInfo = data.data;
            $scope.projectInfo.trunkProject = getTrunkName();

            if ($scope.projectName.indexOf("_") != -1) {
                $scope.projectInfo.branchName = $scope.projectInfo.projectName.substring($scope.projectInfo.trunkProject.length + 1, $scope.projectInfo.projectName.length);
            } else {
                $scope.projectInfo.branchName = $scope.projectInfo.projectName;
            }
            ;
            $http.post(java_url + '/beetle/project/projectInfoList', {
                isShowJar: true,
                packageType: 'maven_jar',
                tokenId: getTokenId()
            }).success(function (data) {
                if (data.code == '0') {
                    $scope.jarProjectList = data.data.list;
                    for (var i = 0; i < $scope.projectInfo.dependency.length; i++) {
                        var info = {};
                        info.jarName = $scope.projectInfo.dependency[i].jarName;

                        for (var j = 0; j < $scope.jarProjectList.length; j++) {
                            if ($scope.jarProjectList[j].projectName != $scope.projectInfo.dependency[i].jarName) {
                                continue;
                            }
                            ;
                            info.jarSvnUrl = $scope.jarProjectList[j].vcsPath;
                        }
                        ;
                        $scope.relateJarList.push(info);
                    }
                    ;
                }
                ;
            });
        });
    } else {
        $http.post(java_url + '/beetle/project/projectInfoList', {
            isShowJar: true,
            packageType: 'maven_jar',
            tokenId: getTokenId()
        }).success(function (data) {
            if (data.code == '0') {
                $scope.jarProjectList = data.data.list;
            }
            ;
        });
    }
    ;

    //根据工程名获取主干名
    function getTrunkName() {
        var projectName = $scope.projectInfo.projectName;
        if (projectName == null || projectName == '') {
            return projectName;
        }
        ;
        if (projectName.indexOf("_") != -1) {
            return projectName.split("_")[0];
        }
        ;
        return projectName;
    };

    $http.post(java_url + '/beetle/project/projectInfoList', {
        isShowJar: true,
        onlyTrunk: true,
        packageType: 'all',
        tokenId: getTokenId()
    }).success(function (data) {
        if (data.code == '2') {
            window.location.href = "/beetle/App/module/login/tpl/login.html";
            return;
        } else if (data.code != '0') {
            alert("获取工程列表失败!desc:" + data.desc);
            return;
        } else {
        }
        ;
        $scope.trunkProjectList = data.data.list;
        if ($scope.projectName == '') {
            $scope.proTypeChange();
        }
    });

    $scope.addJarProject = function () {
        var info = {};
        info.jarName = $scope.jarProjectList[0].projectName;
        info.jarSvnUrl = $scope.jarProjectList[0].vcsPath;
        $scope.relateJarList.push(info);
    };

    $scope.jarProjectChange = function (index) {
        for (var i = 0; i < $scope.jarProjectList.length; i++) {
            if ($scope.jarProjectList[i].projectName == $scope.relateJarList[index].jarName) {
                $scope.relateJarList[index].jarSvnUrl = $scope.jarProjectList[i].vcsPath;
                break;
            }
            ;
        }
        ;
    };
    $scope.delJarProject = function (index) {
        $scope.relateJarList.splice(index, 1);
    };
    $scope.trunkChange = function () {
        for (var i = 0; i < $scope.trunkProjectList.length; i++) {
            if ($scope.trunkProjectList[i].projectName == $scope.projectInfo.trunkProject) {
                $scope.projectInfo.configProjectName = $scope.trunkProjectList[i].configProjectName;
                $scope.projectInfo.packageType = $scope.trunkProjectList[i].packageType;
                $scope.projectInfo.warName = $scope.trunkProjectList[i].warName;
                $scope.projectInfo.packageName = $scope.trunkProjectList[i].packageName;
                break;
            }
            ;
        }
        ;
    };
    $scope.proTypeChange = function () {
        $scope.projectInfo = {};
        $scope.projectInfo.packageType = "maven_war";
        $scope.projectInfo.branchName = "";
        $scope.projectInfo.useSonar = false;
        if ($scope.info.projectType == '1') {
            $scope.projectInfo.trunkProject = $scope.trunkProjectList[0].projectName;
            $scope.trunkChange();
        }
        ;
    };

    $scope.save = function (valid) {
        if (!valid) {
            alert("请正确填写信息!");
            return;
        }

        $scope.runIng = true;

        if ($scope.info.projectType == '0') {
            $scope.projectInfo.projectName = $scope.projectInfo.branchName;
        } else if ($scope.info.projectType == '1') {
            $scope.projectInfo.projectName = $scope.projectInfo.trunkProject + "_" + $scope.projectInfo.branchName;
        }
        ;

        $scope.projectInfo.dependency = [];
        for (var i = 0; i < $scope.relateJarList.length; i++) {
            var info = {};
            info.jarName = $scope.relateJarList[i].jarName;
            info.isDeploy = "0";
            $scope.projectInfo.dependency.push(info);
        }
        ;

        doBeetlePost($http,$uibModal,java_url + '/beetle/project/editProject', {
            projectInfo: JSON.stringify($scope.projectInfo),
            tokenId: getTokenId()
        },function (data) {
            $scope.runIng = false;
            if (data.code == '2') {
                window.location.href = "/beetle/App/module/login/tpl/login.html";
                return;
            } else if (data.code == '0') {
                //1.新增主干工程成功后，调用plan刷新工程列表缓存
                if ($scope.info.projectType == '0') {
                    $http.get(beetle_plan_url + '/project/refreshTrunkList?token_id=' + getTokenId()).success(function (data) {
                        console.log("调用plan刷新工程缓存，返回：" + JSON.stringify(data))
                    });
                }
                window.location.href = "#/menu/projectSet/";
            } else {
                openDialogAlert(ngDialog,$scope,data.desc);
            }
            ;
        });
    };
    $scope.goBack = function () {
        window.location.href = "#/menu/projectSet/";
    };
    $scope.packageTypeChange = function () {
        $scope.projectInfo.branchName = "";
        $scope.projectInfo.configProjectName = "";
        $scope.projectInfo.vcsPath = "";
        $scope.projectInfo.warName = "";
        $scope.projectInfo.projectDesc = "";
        $scope.projectInfo.subModule = "";
        $scope.projectInfo.packageName = "";
        $scope.projectInfo.useSonar = false;
    };
});